/*
===========================================================================
Copyright (C) 2005 Sarunas

This file is part of MyPIRC source code.

DisksDB source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

DisksDB source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with DisksDB; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/
using System;

namespace MyIRC
{
	/// <summary>
	/// Private chat, represents person and chat text
	/// </summary>
	public class Private : BaseObject, IPrivate
	{
		public event EventHandlerDisconnectedPrivate Disconnected;
		public event EventHandlerPrivMsgPrivate PrivateMessage;
		public event EventHandlerNickChangedPrivate NickChanged;
		private IrcUser user = null;

		public Private(IrcClient server, IrcUser user) : base(server)
		{
			this.user = user;
			this.user.NickChanged += new EventHandlerNickChanged(UserNickChanged);
		}

		public void SayTextLine(string text)
		{
			this.server.SendMessage("PRIVMSG " + this.User.Nick + " :" + text);
		}

		public void WhoIs()
		{
			this.server.SendMessage("WHOIS " + this.User.Nick);
		}

		public void Close()
		{
			this.server.Remove(this);
		}

		public IUser User
		{
			get
			{
				return this.user;
			}
		}

		public override void OnDisconnected()
		{
			base.OnDisconnected();

			if (null != this.Disconnected)
			{
				this.Disconnected(this.server, this);
			}
		}

		public virtual void OnPrivateMessage(string fullId, string message)
		{
			if (null != this.PrivateMessage)
			{
				this.PrivateMessage(this.server, this, this.server.FindUser(fullId, true), message);
			}
		}

		public virtual void OnNickChanged(string oldNick, string newNick)
		{
			if (null != this.NickChanged)
			{
				this.NickChanged(this.server, this, oldNick, newNick);
			}
		}

		private void UserNickChanged(IConnection server, IUser user, string oldNick, string newNick)
		{
			this.OnNickChanged(oldNick, newNick);
		}
	}
}
